package com.shs.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.bus.entity.Customer;
import com.shs.bus.service.CustomerService;
import com.shs.bus.vo.CustomerVo;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import com.shs.sys.common.WebUtils;
import com.shs.sys.entity.Notice;
import com.shs.sys.entity.User;
import com.shs.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  客户管理前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-16
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 查询客户
     * @return
     */
    @RequestMapping("loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo vo){
        Page<Customer> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Customer> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(vo.getCustomername()),"customername",vo.getCustomername());
        wrapper.like(StringUtils.isNotBlank(vo.getTelephone()),"telephone",vo.getTelephone());
        wrapper.like(StringUtils.isNotBlank(vo.getConnectionperson()),"connectionperson",vo.getConnectionperson());
        wrapper.orderByDesc("id");
        customerService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());

    }

    /**
     * 添加客户
     * @param vo
     * @return
     */
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(CustomerVo vo){
        try {
            customerService.save(vo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改客户
     * @param vo
     * @return
     */
    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(CustomerVo vo){
        try {
            customerService.updateById(vo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(Integer id){
        try {
            customerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param vo
     * @return
     */
    @RequestMapping("batchDeleteCustomer")
    public ResultObj batchDeleteCustomer(CustomerVo vo){
        List<Integer> idsList = new ArrayList<>();
        try {
            for (Integer id : vo.getIds()) {
                idsList.add(id);
            }
            customerService.removeByIds(idsList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


}
